package com.ruoyi.web.controller.api;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.domain.WhOrder;
import com.ruoyi.system.domain.params.PrinterParam;
import com.ruoyi.system.domain.params.StorehousePrintParams;
import com.ruoyi.system.domain.vo.CounterEditInfoVo;
import com.ruoyi.system.domain.vo.CounterListVo;
import com.ruoyi.system.domain.vo.ManagerOrderListVo;
import com.ruoyi.system.domain.vo.OrderInfoVo;
import com.ruoyi.system.mapper.WhOrderMapper;
import com.ruoyi.system.service.IWhCounterService;
import com.ruoyi.system.service.IWhOrderService;
import com.ruoyi.system.service.PrintService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 陈平安
 * @date: 2023/3/16 22:05
 * @description:
 */
@RestController
@RequestMapping("/manager")
public class ManagerController extends BaseController {

    @Resource
    private SysLoginService loginService;
    @Resource
    private IWhOrderService orderService;
    @Resource
    private WhOrderMapper orderMapper;
    @Resource
    private IWhCounterService counterService;
    @Resource
    private PrintService printService;

    /**
     * 仓管、财务登录
     * @param params
     * @return
     */
    @PostMapping("/login")
    public AjaxResult managerLogin(@RequestBody Map<String,String> params) {
        String token = loginService.login(
                MapUtils.getString(params,"account",""),
                MapUtils.getString(params,"password",""),
                Constants.COMMON_CODE,
                null
        );
        return AjaxResult.success("登录成功", token);
    }

    /**
     * 获取用户信息
     * @return 用户信息
     */
    @GetMapping("/userInfo")
    public AjaxResult getInfo() {
        Map<String,Object> result = new HashMap<>();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user != null) {
            result.put("userId",user.getUserId());
            result.put("nickname",user.getNickName());
            result.put("username",user.getUserName());
        }
        return AjaxResult.success("获取成功", result);
    }

    /**
     * 仓管端获取订单列表
     * @param keyword
     * @return
     */
    @GetMapping("/order/list")
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    public TableDataInfo findCustomerCounter(String keyword) {
        startPage();
        List<ManagerOrderListVo> list = orderService.findManagerOrderList(keyword);
        return getDataTable(list);
    }

    /**
     * 仓管端获取订单列表
     * @param orderNo 订单编号
     * @return
     */
    @GetMapping("/order/info/{orderNo}")
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    public AjaxResult getOrderInfoById(@PathVariable("orderNo") String orderNo) {
        WhOrder order = orderMapper.selectWhOrderByOrderNo(orderNo);
        if (order == null) {
            return error("订单不存在");
        }
        OrderInfoVo orderInfo = orderService.getOrderInfoById(order.getId());
        if (orderInfo == null) {
            return error("订单不存在");
        }
        return success(orderInfo);
    }

    /**
     * 仓管端获取订单列表
     * @return
     */
    @PostMapping("/order/print")
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    public AjaxResult printOrder(@RequestBody PrinterParam param) {
        param.setCreateBy(getUserId() + "");
        orderService.printOrderV2(param);
        return success("已加入打印队列");
    }

    /**
     * 手机财务、仓管端货柜
     * @param keyword
     * @return
     */
    @GetMapping("/container/list")
    @PreAuthorize("@ss.hasPermi('system:counter:list')")
    public TableDataInfo containerList(String keyword) {
        startPage();
        List<CounterListVo> list = counterService.findCounterList(keyword);
        return getDataTable(list);
    }

    /**
     * 手机财务、仓管端货柜编辑详情
     * @param id 货柜id
     * @return
     */
    @GetMapping("/container/info/{id}")
    @PreAuthorize("@ss.hasPermi('system:counter:list')")
    public AjaxResult getCounterEditInfo(@PathVariable("id") Integer id) {
        CounterEditInfoVo counterEditInfo = counterService.getCounterEditInfo(id);
        return success(counterEditInfo);
    }

    /**
     * 打印包裹单
     * @return
     */
    @PostMapping("/print/package")
    @PreAuthorize("@ss.hasPermi('system:counter:list')")
    public AjaxResult printPackage(@RequestBody StorehousePrintParams params) {
        try {
            printService.storehousePrint(params);
            return success("已加入打印队列");
        } catch (Exception e) {
          return error(e.getMessage());
        }
    }

}
